import app from "../../app"
import Vue from "vue"
import Vuex from "vuex"
import * as type from"./types"

Vue.use(Vuex);

const store = ()=> new Vuex.Store({
  state: {
    site: {
      name: "",
      email: "",
      api: "",
      apiVersion: ""
    },
    token: "",
    user: {
      username: "",
      avatar: ""
    }
  },
  mutations: {
    [ type.SET_SITE ] (state, data) {
      state.site = data
    },
    [ type.SET_TOKEN ] (state, data) {
      state.token = data
    },
    [ type.SET_USER ] (state, data) {
      state.user = data
    }
  },
  actions: {
    // 初始化数据
    async nuxtServerInit({ dispatch }, { req }) {
      const resp = await this.$axios.$get(`${app.api}/api/${app.apiVersion}/base/`);
      dispatch(type.SET_SITE, resp)
    },
    [ type.SET_SITE ] ({ commit }, data) {
      commit(type.SET_SITE, data)
    },
    [ type.SET_TOKEN ] ({ commit }, data) {
      commit(type.SET_TOKEN, data)
    },
    [ type.SET_USER ] ({ commit }, data) {
      commit(type.SET_USER, data)
    }
  }
});

export default store
